package com.xuanyuanxinyu.func.network.retrofit

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest

/**
 * 网络监听管理
 */
class NetworkConnecManager @SuppressLint("WrongConstant") constructor(context : Context) {
    companion object {
        private const val TAG = "PublicDnsManager"
        private const val PUBLIC_DNS_SERVER = "8.8.8.8" // 示例公共DNS服务器
    }
    
    private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    private val networkRequestBuilder = NetworkRequest.Builder()
    private val networkCallback : NetworkCallback
    
    init {
        networkCallback = NetworkCallback()
        //        networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        //        networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
        // 设置网络请求的类型，这里指定了传输层协议为TCP
        networkRequestBuilder.addCapability(NetworkCapabilities.TRANSPORT_CELLULAR)
            .addCapability(NetworkCapabilities.TRANSPORT_WIFI)
        // 注册网络回调
        connectivityManager.requestNetwork(networkRequestBuilder.build(),networkCallback)
    }
    
    fun registerCallback(networkRequest : NetworkRequest?) {
        // 取消注册网络回调
        connectivityManager.requestNetwork(networkRequestBuilder.build(),networkCallback)
    }
    
    fun unregisterCallback() {
        // 取消注册网络回调
        connectivityManager.unregisterNetworkCallback(networkCallback)
    }
    
    val activeNetwork : Unit
        get() {
            val activeNetwork = connectivityManager.activeNetwork
            val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
        }
    
    
    private class NetworkCallback : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network : Network) {
            // 当网络可用时，更新DNS
        }
    }
    
    
}


